

class Slider {
  constructor() {
    this.touchOrigin = 0;
    this.time = 0;
    this.interval = "";
    this.timer = ""
  }

  //e 对应页面中 相应时间的 event值
  touchStart(e) {
    this.touchOrigin = e.touches[0].pageX;
    this.interval = setInterval(() => {
      this.time++
    }, 100);
  }

  touchMove(e,cb) {
    const touchMove = e.touches[0].pageX;
    const distance = touchMove - this.touchOrigin;
    if (distance <= -40 && this.time < 10) {

      clearTimeout(this.timer);
      this.timer = setTimeout(function(){
        cb && cb('left')
      }, 100);

    }
    if (distance >= 40 && this.time < 10) {
      clearTimeout(this.timer);
      this.timer = setTimeout(function(){
        cb && cb('right')
      }, 100);
    }
  }

  touchEnd(e) {
    clearInterval(this.interval); // 清除setInterval
  
    this.time = 0;
  }
}

const classSlider = new Slider();

export { classSlider }